package com.example.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;


import java.util.List;

public class MyContentProvider extends ContentProvider {
    private static  final int OP_DIR=0xf1;
    private static final int OP_ITEM=0xf2;
    private static final String MIME_DIR = "vnd.android.cursor/com.example.student.dir";
    private static final String MIME_ITEM ="vnd.android.cursor/com.example.student.item";
    private  DBHelper helper;
    private static UriMatcher matcher;
    static {
        matcher=new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI(StuUri.AUTHORTIES,"/deleteStu",OP_DIR);
        matcher.addURI(StuUri.AUTHORTIES,"/deleteStu/#",OP_ITEM);
    }
    

    public MyContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        List<String> pathList=uri.getPathSegments();
        //获取uri  path部分最后一位
        String deleteOpFlag=pathList.get(pathList.size()-1);
        switch (getType(uri)){
            case MIME_ITEM:

        }

        switch (deleteOpFlag){
            case "deleteStu":
                if(TextUtils.isEmpty(selection)){
                    if(!selection.contains("and")&&!selection.contains("or")){
                        if (selection.contains(helper.STU_NAME)){
                            long id=helper.deleteStuName(selection);
                        }
                    }
                }
        }
        return -1;
    }

    @Override
    public String getType(Uri uri) {
        switch (matcher.match(uri)){
            case OP_DIR:
                return MIME_DIR;
            case  OP_ITEM:
                return MIME_ITEM;
                
            default:
                return null;
        }
    }
//当外部通过ContentResolver调用insert()方法时触发
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        //回去uri的path部分，以/分隔而成的字符串List
        List<String> pathList=uri.getPathSegments();
        //获取uri  path部分最后一位
        String insertOpFlag=pathList.get(pathList.size()-1);

        switch (insertOpFlag){
            case "addStu":
               long id= helper.insertStuInfo(values);
               //"content://"+StuUri.AUTHORTIES+"/"+id)表示单条数据
               return Uri.parse("content://"+StuUri.AUTHORTIES+"/"+id);
        }
        return null;
    }

    @Override
    public boolean onCreate() {
        // ContentProvider初始化的回调方法
        //当onCreate()返回true时，表示创建成功   创建成功后不会重复创建
        helper=new DBHelper(getContext(),"student.db",null,1);
        return helper.getWritableDatabase()!=null;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // TODO: Implement this to handle query requests from clients.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}